<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Genetic Programming System by Paras Chopra</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div align="center"><font size="5" face="Arial, Helvetica, sans-serif"><strong>Genetic 
  Programming System</strong></font></div>
<p><font face="Arial, Helvetica, sans-serif">By Paras Chopra<br>
  <a href="http:/www.paraschopra.com">http://www.paraschopra.com</a><br>
  Email: paras1987 [at] gmail [dot] com<br>
  Email: paraschopra [at] paraschopra [dot] com</font></p>
<p><font face="Arial, Helvetica, sans-serif"><em>Note:</em> You may use this Genetic 
  Programming module in any way you wish. But please do not forget to give me 
  the credit which I deserve. And please report if you debug it or enhance it 
  or apply it for real world problems.</font></p>
<hr>
<p><font face="Arial, Helvetica, sans-serif"><strong>Sample Usage example</strong></font></p>
<pre>prg = Programs({COS:1,RANDOM:0}, [1,2],[&quot;A&quot;,&quot;B&quot;]</pre>

<p><font face="Arial, Helvetica, sans-serif"><strong>Syntax of the program</strong><br>
  
<pre>pr=Programs(FuncDict,TerminalList,Variable,MaxDepth=10,Population=100,MaxGen=100,ReqFitness=99,CrossRate=0.9,MutRate=0.1,BirthRate=0.2,HighFitness=100)
pr.MainLoop()</pre>
</font>
<p><font face="Arial, Helvetica, sans-serif">Or <em>pr=Programs( {function1: no_of_arguments_of_function,...} 
  , [list of leafs or constants], [list of variable names] )</em></font></p>
<p><font face="Arial, Helvetica, sans-serif"><strong>Description of arguments</strong></font></p>
<ul>
  <li><font face="Arial, Helvetica, sans-serif"><strong>FuncDict</strong> is the 
    dictionary of actual function names and the number of arguments it takes </font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>TerminalList</strong> 
    is the list of terminal constants possible in the tree e.g. [1,2,5,6] or range(5,11) 
    or [1,2,&quot;random&quot;]<br>
    </font><font face="Arial, Helvetica, sans-serif">&quot;<em>random</em>&quot; 
    in the Terminal List produces a number between 0 and 1 and e.g. 0.257522 or 
    0.444621</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>Variable</strong> is a 
    list of possible variables in the tree e.g. [&quot;X&quot;,&quot;Y&quot;] 
    or [&quot;A&quot;,&quot;B&quot;]</font></li>
  <li><font face="Arial, Helvetica, sans-serif">It is the responsibility of fitness 
    function to supply values to the variables by using syntax:<br>
    </font><font face="Arial, Helvetica, sans-serif">Prog.Variables.SetVal(Variable_Name,Variable_Value) 
    e.g Prog.Variables.SetVal(&quot;X&quot;,10)</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>MaxDepth</strong> is the 
    maximum depth allowed for the initial trees</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>Population</strong> is 
    population in each generation. It starts from 0 to 99 i.e If u want 100 individuals 
    then pass 99 as parameter</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>MaxGen</strong> is the 
    maximum number of generations until the evolution is aborted</font></li>
  <li><strong><font face="Arial, Helvetica, sans-serif">ReqFitness</font></strong><font face="Arial, Helvetica, sans-serif"> 
    is the fitness level above which if any program is possesing fitness the program 
    is terminated<br>
    </font><font face="Arial, Helvetica, sans-serif">In the default case it is 
    99, i.e if any program has fitness greater than 99, the evolution is aborted 
    and the candidate is termed as best</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>CrossRate</strong> is 
    the crossover rate, its default value is 0.9 i.e. the crossover is bound to 
    happen 90% of time</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>MutRate</strong> is the 
    rate of mutation</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>BirthRate</strong> is 
    the number of new individuals produced per unit of population<br>
    </font><font face="Arial, Helvetica, sans-serif">Its default value is 0.2 
    i.e if the population is 100 then 20 children will be produced per crossover 
    operation</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>HighFitness</strong> is 
    the highest fitness attainable by the candidate, in default case it is 100</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>MaxGen</strong> is maximum 
    number of generations</font></li>
  <li><font face="Arial, Helvetica, sans-serif"><strong>BirthRate</strong> is 
    no of offsprings per 100 population e.g. if BirthRate is 2 and population 
    of current population is 100 then in the next generation only 2 offsprings 
    will be produced</font></li>
</ul>
<p><font face="Arial, Helvetica, sans-serif"><strong>Sample Usage example</strong><br>
  prg = Programs({COS:1,RANDOM:0}, [1,2],[&quot;A&quot;,&quot;B&quot;])</font></p>
<p><font face="Arial, Helvetica, sans-serif"><strong>To define functions of your 
  own</strong><br>
  The functions used in the trees are real world python functions. So of you want 
  to add a new function such as power(a,b) i.e to calculate a^b<br>
  use the following syntax:</font></p>
<pre><font color="#C00000">def</font> <font color="#000000">POWER</font><font color="#0000C0">(</font><font color="#000000">ValuesList</font><font color="#0000C0">)</font><font color="#0000C0">:</font>
    <font color="#000000">ans</font><font color="#0000C0">=</font><font color="#000000">ValuesList</font><font color="#0000C0">[</font><font color="#0080C0">0</font><font color="#0000C0">]</font>
    <font color="#C00000">if</font> <font color="#000000">ValuesList</font><font color="#0000C0">[</font><font color="#0080C0">1</font><font color="#0000C0">]</font><font color="#0000C0">&lt;</font><font color="#0080C0">0</font><font color="#0000C0">:</font>
        <font color="#C00000">return</font> <font color="#0080C0">0</font>
    <font color="#C00000">for</font> <font color="#000000">i</font> <font color="#C00000">in</font> <font color="#000000">range</font><font color="#0000C0">(</font><font color="#0080C0">0</font><font color="#0000C0">,</font><font color="#000000">ValuesList</font><font color="#0000C0">[</font><font color="#0080C0">1</font><font color="#0000C0">]</font><font color="#0000C0">)</font><font color="#0000C0">:</font>
        <font color="#000000">ans</font><font color="#0000C0">=</font><font color="#000000">ans</font><font color="#0000C0">*</font><font color="#000000">ValuesList</font><font color="#0000C0">[</font><font color="#0080C0">0</font><font color="#0000C0">]</font>
    <font color="#C00000">return</font> <font color="#000000">ans</font>
</pre>
<p><font face="Arial, Helvetica, sans-serif">The functions which you will define 
  will always contain only one argument which is ValuesList. ValuesList is the 
  list of values passed to the function. In the present case of a^b ValuesList 
  will contain values of a and b. So ValuesList[0] will represent the first value 
  i.e a and ValuesList[1] will represent the second value i.e b.</font></p>
<p><font face="Arial, Helvetica, sans-serif">If your function takes three values 
  then you will also use ValuesList[2] and If your function does not takes any 
  values such as RANDOM() then the list will be empty</font></p>
<p><font face="Arial, Helvetica, sans-serif">But observe that only one value can 
  be returned from the function</font></p>
<p><font face="Arial, Helvetica, sans-serif"><strong>Note</strong><br>
  You may also use this module to create instances of many GPs running simultaneously. 
  Or use it to run GP elsewhere in your program<br>
  </font> </p>
</body>
</html>
